<template>
<div class="folder-explorer">
<div class="content">
  <div class="toolbar">

    <div class="bar">
      <div @click="handleBack" class="back"><i class="el-icon-back"></i></div>
    <template v-if="!edit">
      <div class="folder-input w">
        <div v-for="(item, index) in pathArray"
             :key="index"
        >{{item}}</div>
      </div>
    </template>
      <template v-else>
        <input v-focus @blur="edit=false" v-on:keyup.enter="handleInput" ref="text" class="w" type="text" v-model="path"/>
      </template>
      <el-popover
        placement="bottom"
        title="创建目录"
        width="200"
        trigger="click"
        v-model="visible">
        <div style="text-align: right; margin: 0">
          <el-input v-model="createFolder">
          </el-input>
          <el-button size="mini" type="text" @click="visible = false">取消</el-button>
          <el-button type="primary" size="mini" @click.native="handleAddClick">确定</el-button>
        </div>
        <div
          slot="reference"
          class="edit-icon"><i class="el-icon-folder-add"></i>
        </div>
      </el-popover>

      <div
        @click="handleEditClick"
        class="edit-icon"><i class="el-icon-edit"></i>
      </div>
    </div>
    <div class="show-folder w">
      <div v-for="(item, index) in files" :key="index" @click="handleFile(item)" class="folder">
       <div class="icon">
         <i :class="item.folder?'el-icon-folder':'el-icon-document'"></i>
       </div>
        <div class="folder-info">{{item.name}}</div>
      </div>
    </div>

  </div>
  <div @click="handleClickButton" class="button">
    在此创建项目
  </div>
</div>

</div>
</template>

<script>
export default {
  name: 'Project',
  data () {
    return {
      api: {
        detail: this.$api.project.detail,
        created: this.$api.project.created
      },
      path: null,
      edit: false,
      files: [
        // {
        //   folder: true,
        //   name: '小黄片'
        // }
      ],
      visible: false,
      createFolder: null
    }
  },
  computed: {
    pathArray () {
      if (!this.path) {
        return []
      }
      if (this.path.includes('/')) {
        return this.path.split('/')
      }
      return this.path.split('\\')
    },
    createdPath () {
      let pathArray = this.pathArray.slice()
      pathArray.push(this.createFolder)
      return pathArray.join('\\')
    }
  },
  methods: {
    handleEditClick () {
      this.edit = true
    },
    handleInput () {
      this.handleFiles()
    },
    handleClickButton () {
      this.$emit('handlePath', this.path)
      this.$emit('handleTabs', 1)
      this.$router.push('/basic')
    },
    handleFiles () {
      this.$httpf({ url: this.api.detail, paras: { path: this.path } }, (data) => {
        this.path = data.data.data.detail.path
        this.files = data.data.data.detail.files
      })
    },
    handleFile (file) {
      if (!file.folder) {
        this.$message.error('此文件不是目录')
        return
      }
      this.path = this.path + '/' + file.name
      this.handleFiles()
    },
    handleBack () {
      let pathArray = this.pathArray.slice()
      pathArray[pathArray.length - 1] = ''
      this.path = pathArray.join('\\')
      this.handleFiles()
    },
    handleAddClick () {
      let path = this.createdPath
      this.$httpf({ url: this.api.created, paras: { path: path } }, (data) => {
        this.visible = false
        this.path = path
        this.handleFiles()
      })
    }
  },
  directives: {
    focus: {
      // 指令的定义
      inserted: function (el) {
        el.focus()
      }
    }
  },
  created () {
    this.handleFiles()
  }
}
</script>

<style scoped lang="less">
.folder-explorer {
  width: 1168px;
  height: 100%;
  margin: auto;
  .w {
    width: 940px;
  }
  .content {
    display: flex;
    height: 100%;
    flex-direction: column ;
    box-sizing: border-box;
    padding-top: 35px;

    .toolbar {
      overflow: auto;
      flex: 1 1 100%;
      .bar {
        display: flex;
        background-color: rgb(228, 245, 239);
        width: 940px;
        border-radius: 3px;
        margin: auto;
        margin-bottom: 20px;
        .back {
          width: 30px;
          font: 16px/32px Roboto, Avenir, Helvetica, Arial, sans-serif;
          text-align: center;
          cursor: pointer;
        }
      }
      input {
        padding: 0 10px;
        font-size: 14px;

      }
      input:focus {
        background: white;
        border: none;
        border-bottom: 1px solid rgb(66, 185, 131);
        outline: none;
      }
      .folder-input {
        display: flex;
        height: 32px;
        background-color: rgb(228, 245, 239);
        div {
          padding: 0 10px;
          font: 14px/32px Roboto, Avenir, Helvetica, Arial, sans-serif;
          border-radius: 2px;
          border-left: 2px solid white;
        }
      }
      .show-folder {
        height: 24px;
        padding: 16px;
        margin: auto;

        .folder {
          display: flex;
          padding-left: 35px;
          cursor: pointer;
          height: 56px;
          .icon {
            width: 24px;
            height: 24px;
            font-size: 24px;
            color: rgb(66, 185, 131);
            padding-top: 11px;
          }
          .folder-info {
            font-size: 16px;
            line-height: 56px;
            margin-left: 10px;
          }
        }
        .folder:hover {
          color: #42b983;
          background: rgba(66, 185, 131, .1);
        }

      }
      .edit-icon {
        width: 32px;
        height: 32px;
        margin-left: 3px;
        font: 12px/32px Roboto, Avenir, Helvetica, Arial, sans-serif;
        cursor: pointer;
      }
    }
    .button {
      margin: auto;
      width: 190px;
      height: 44px;
      font: 16px/44px Roboto, Avenir, Helvetica, Arial, sans-serif;
      color: white;
      background-color: #42b983;
      text-align: center;
      cursor: pointer;
    }
    .button:hover {
      background-color: rgba(66, 185, 132, .8);
    }
  }

}
</style>
